/**
 * 用户信息模块
 */

import {reqUserInfo} from '@/api/user.js';  //引入请求函数

const state = {
    user_token:"",
    userInfo:{}
};

const getters = {
    getToken(){
        //返回本地存储的token
        return localStorage.getItem('user_token');
    },
    getUserName(state){
        return state.userInfo.username;
    },
    getUserNick:state => state.userInfo.nickname,
    getAvatar:state => state.userInfo.user_pic,
};

const actions = {
    set_token(context,playload){
        context.commit('SET_TOKEN',playload);
        localStorage.setItem("user_token",playload.token);
    },
    get_userinfo(context,playload){
        context.commit("GET_USERINFO",playload);
    },
    //更新user模块中userInfo数据
    async updateUserInfo(){
        let res = await reqUserInfo();
        if(res.code === 0){
            state.userInfo = res.data;
        }
    }
};

const mutations = {
    SET_TOKEN(state,payload){
        state.user_token = payload.token;
    },
    CLEAR_TOKEN(state){
        state.user_token = "";
        localStorage.removeItem("user_token");
    },
    GET_USERINFO(state,playload){
        state.userInfo = playload;
    }
};

export default{
    namespaced:"true",
    state,
    getters,
    actions,
    mutations
}